<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'getlist';
$return['basic']=$this->plugin_setting('basic');
//exi('','','jy_weishop/order.list/');
if($_RQ['op']=='main'){
	$return['basic']=$this->plugin_setting('basic');
	$return['list']=array();
	set_html('title','订单列表');
	exi($return);
}
// js 获取数据
if($_RQ['op']=='getlist'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']);
	if(isset($_RQ['status']) && $_RQ['status'] != 100){
		$params['status']=intval($_RQ['status']);
		$return['status']=$_RQ['status'];
	}else{
		$return['status']=100;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_aligroup_groups_order',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	foreach($list as &$item){
		$goodsid=pdo_getcolumn('jy_aligroup_groups_order',array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'goodsid');
		$item['goods']=pdo_get('jy_weishop_goods',array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
		$item['goods']['image']=tomedia($item['goods']['image']);
		$item['store']=pdo_get('jy_weishop_store',array('id'=>$item['storeid'],'uniacid'=>$_SESSION['uniacid']));
		$item['createtime']=date("Y-m-d H:i",$item['createtime']);
		$item['statustitle']=$this->order_status[$item['status']];
	}
	$return['list']=$list;
	$return['params']['status']=$return['status'];
	//exi(count($list),'error');exit;
	exi($return);
}
if($_RQ['op']=='pay'){
	$id = intval($_RQ['id']);
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	$pay_id=pdo_getcolumn('jy_aligroup_groups_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id),'pay_id');
	exi($pay_id);
}
if($_RQ['op']=='receive'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_update('jy_aligroup_groups_order',array('status'=>4),array('id'=>$_RQ['id']));
	exi('确认订单成功！');
}
if($_RQ['op']=='delete'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_delete('jy_aligroup_groups_order',array('id'=>$_RQ['id']));
	exi('订单删除成功');
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['payresult'];unset($_SESSION['payresult']);
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','list');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','list');
	}
	$order=pdo_get('jy_aligroup_groups_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	
	pdo_update('jy_aligroup_groups_order',array('status'=>1),array('id'=>$order['id']));
	//支付成功，判断拼团人数是否达到，人数达到则更改所有该拼团订单的状态
	$num=pdo_count('jy_aligroup_groups_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'groupid'=>$order['groupid'],'teamid'=>$order['teamid']));
	$group=pdo_get('jy_aligroup_groups',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['groupid']));
	if($group['groupnum']==$num){
		$data=array(
			'status'=>2,
			'success'=>3,
			'finishtime'=>TIMESTAMP
		);
		pdo_update('jy_aligroup_groups_order',$data,array('uniacid'=>$_SESSION['uniacid'],'groupid'=>$order['groupid'],'teamid'=>$order['teamid']));
		exi('支付成功','success','jy_aligroup/order/getlist/status=2');
	}
	exi('支付成功','success','jy_aligroup/order/getlist/status=1');
}	
?>